# -*- coding: utf-8 -*-

from random import randint
from itertools import chain, islice
from index.models import BookComment, Book, CommentLike

def grouper(items, step):
    it = iter(items)
    while True:
        chunk = list(islice(it, step))
        if not chunk:
            break
        yield chunk


def get_book_ids():
    return Book.objects.values_list("id", flat=True).all()


def get_bcs(book_ids):
    bcs = [
        [
            BookComment(
                user_id=randint(20002, 20010), book_id=bid, score=randint(1, 5),
                content="这本书说真的一言难尽，懂得都懂，里面涉及的太多了，不能透漏太多"
            ) for _ in range(randint(150, 300))
        ] for bid in book_ids
    ]
    return list(chain(*bcs))


def get_comment_ids(limit=100000):
    return list(BookComment.objects.values_list("id", "book_id"))


def get_likes(comment_ids):
    for _comment_ids in grouper(comment_ids, 10):
        likes = [
            CommentLike(user_id=i+10, book_id=idid[1], comment_id=idid[0]) for i in range(randint(20, 300))
            for idid in _comment_ids
        ]
        yield likes
